package com.qianyu.project.web.controller.wyh;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.LaborSubcontract;
import com.qianyu.project.entity.ProviderType;
import com.qianyu.project.entity.QuantitiesList;
import com.qianyu.project.service.ILaborSubcontractService;
import com.qianyu.project.service.IQuantitiesListService;
import com.qianyu.project.service.ISubcontractType1;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author wyh
 * Date 2021/2/4
 **/
@RestController
@RequestMapping("WyhSubcontracts")
public class WyhLaborSubcontractController {

    @Autowired
    private ILaborSubcontractService iLaborSubcontractService;

    @Autowired
    private ISubcontractType1 iSubcontractType1;

    @Autowired
    private IQuantitiesListService iQuantitiesListService;

    @GetMapping("")
    public Result getContractTypeList(){
        return ResultTool.success(iSubcontractType1.list());
    }

    @PostMapping("")
    public Result addLaborSubcontract(@RequestBody  Map<String,Object> adds){
        System.out.println(adds);
        LaborSubcontract ls = new LaborSubcontract();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        ls.setId(Integer.valueOf(mapls.get("id")+""));
        ls.setPId(Integer.valueOf(mapls.get("pId")+""));
        ls.setContractName(mapls.get("contractName")+"");
        ls.setContractType(Integer.valueOf(mapls.get("contractType")+""));
        ls.setProId(Integer.valueOf(mapls.get("proId")+""));
        ls.setEId(Integer.valueOf(mapls.get("eId")+""));
        ls.setStartDate(mapls.get("startDate")+"");
        ls.setEndDate(mapls.get("endDate")+"");
        ls.setContractAmount(Long.valueOf(mapls.get("contractAmount")+""));
        ls.setAmountUpper(mapls.get("amountUpper")+"");
        ls.setPaymentType(mapls.get("paymentType")+"");
        ls.setSettleType(mapls.get("settleType")+"");
        ls.setAdvancePay(BigDecimal.valueOf(Double.valueOf(mapls.get("advancePay")+"")));
        ls.setDepositAmount(BigDecimal.valueOf(Double.valueOf(mapls.get("depositAmount")+"")));
        ls.setApproveStatus(1+"");
        ls.setPayCondition(mapls.get("payCondition")+"");
        ls.setMainTerm(mapls.get("mainTerm")+"");
        ls.setComment(mapls.get("comment")+"");
        ls.setAttachIds(mapls.get("attachIds")+"");
        ls.setCreateDate(mapls.get("createDate")+"");
        iLaborSubcontractService.saveOrUpdate(ls);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<QuantitiesList> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            QuantitiesList qq = new QuantitiesList();
            qq.setPId(Integer.valueOf(1+""));
            qq.setSubList(i.get("subList")+"");
            qq.setUnit(i.get("unit")+"");
            qq.setWorkCount(Integer.valueOf(i.get("workCount")+""));
            qq.setComprePrice(BigDecimal.valueOf(Double.valueOf(20+"")));
            qq.setSumPrice(BigDecimal.valueOf(Double.valueOf(i.get("sumPrice")+"")));
            qq.setComment(123+"");
            qq.setSubId(ls.getI());
            aiList.add(qq);
        });
        iQuantitiesListService.saveBatch(aiList);
        return ResultTool.success();
    }

    @GetMapping("/list")
    public Result getSubcontracts(@RequestParam Map<String,Object> map){

        return ResultTool.success(iLaborSubcontractService.getSubcontracts(map));
    }

    @GetMapping("/code")
    public Result getMaxCode() {
        return ResultTool.success(Long.parseLong(iLaborSubcontractService.getMaxCode()) + 1);
    }

    @DeleteMapping("/{i}")
    public Result deleteByIdSubcontract(@PathVariable String i){
        iLaborSubcontractService.removeById(i);
        iQuantitiesListService.remove(new QueryWrapper<QuantitiesList>().eq("sub_id",i));
        return  ResultTool.success();
    }

    @GetMapping("/getList/{id}")
    public Result getByIdSubcontract(@PathVariable String id){
        Map<String, Object> map =new HashMap<>();
        LaborSubcontract sub = iLaborSubcontractService.getTo(id);
        map.put("laborSubcontract", sub);
        List<QuantitiesList> quan = iQuantitiesListService.list(new QueryWrapper<QuantitiesList>().eq("sub_id", id));
        map.put("quantitiesList", quan);
        return ResultTool.success(map);

    }

    @PutMapping("/update")
    public Result updateSubcontract(@RequestBody  Map<String,Object> adds){
        System.out.println(adds);
        LaborSubcontract ls = new LaborSubcontract();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        ls.setI(Integer.valueOf(mapls.get("i")+""));
        ls.setId(Integer.valueOf(mapls.get("id")+""));
        ls.setPId(Integer.valueOf(mapls.get("pId")+""));
        ls.setContractName(mapls.get("contractName")+"");
        ls.setContractType(Integer.valueOf(mapls.get("contractType")+""));
        ls.setProId(Integer.valueOf(mapls.get("proId")+""));
        ls.setEId(Integer.valueOf(mapls.get("eId")+""));
        ls.setStartDate(mapls.get("startDate")+"");
        ls.setEndDate(mapls.get("endDate")+"");
        ls.setContractAmount(Long.valueOf(mapls.get("contractAmount")+""));
        ls.setAmountUpper(mapls.get("amountUpper")+"");
        ls.setPaymentType(mapls.get("paymentType")+"");
        ls.setSettleType(mapls.get("settleType")+"");
        ls.setAdvancePay(BigDecimal.valueOf(Double.valueOf(mapls.get("advancePay")+"")));
        ls.setDepositAmount(BigDecimal.valueOf(Double.valueOf(mapls.get("depositAmount")+"")));
        ls.setApproveStatus(1+"");
        ls.setPayCondition(mapls.get("payCondition")+"");
        ls.setMainTerm(mapls.get("mainTerm")+"");
        ls.setComment(mapls.get("comment")+"");
        ls.setAttachIds(mapls.get("attachIds")+"");
        ls.setCreateDate(mapls.get("createDate")+"");
        iLaborSubcontractService.saveOrUpdate(ls);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<QuantitiesList> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            QuantitiesList qq = new QuantitiesList();
            qq.setId(Integer.valueOf(i.get("id")+""));
            qq.setSubList(i.get("subList")+"");
            qq.setUnit(i.get("unit")+"");
            qq.setWorkCount(Integer.valueOf(i.get("workCount")+""));
            qq.setComprePrice(BigDecimal.valueOf(Double.valueOf(20+"")));
            qq.setSumPrice(BigDecimal.valueOf(Double.valueOf(i.get("sumPrice")+"")));
            qq.setComment(123+"");
            qq.setSubId(ls.getI());
            aiList.add(qq);
        });
        iQuantitiesListService.saveOrUpdateBatch(aiList);
        return ResultTool.success();
    }

}
